<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\property\upload;

use app\controller\property\Base;

use api\upload\exception\UploadException;
use app\common\repositories\system\upload\SystemUploadFileRepository;
use app\common\repositories\system\upload\SystemUploadGroupRepository;

class UploadGroup extends Base
{


    /**
     * 选择上传文件
     * @param SystemUploadGroupRepository $repository
     * @return string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function list(SystemUploadGroupRepository $repository)
    {
        if ($this->request->isAjax()) {
            $data = [];
            if ($this->request->propertyUserInfo['is_main'] != 1) {
                $data = [
                    'user_id' => $this->request->propertyUserInfo['user_id'],
                ];
            }
            $groupList = $repository->getCascadesData($this->request->propertyId, $data);
            return json()->data(['code' => 0, 'data' => $groupList]);
        }
        return $this->fetch('upload/select_upload_file');
    }


    public function add(SystemUploadGroupRepository $repository)
    {
        if ($this->request->isPost()) {
            $name = $this->request->param('name', '', 'trim');
            if (empty($name)) {
                return json()->data(['code' => -1, 'msg' => '参数错误']);
            }
            $pid = (int)$this->request->param('pid', 0);
            if ($pid && !$repository->exists($pid)) {
                return $this->error('上级分类不存在');
            }
            $data = [
                'group_name' => $name,
                'property_id' => $this->request->propertyId,
                'user_id' => $this->request->propertyUserInfo['user_id'],
                'pid' => $pid
            ];
            $res = $repository->create($data);
            if ($res) {
                return json()->data(['code' => 0, 'msg' => '添加成功', 'data' => ['id' => $res->id]]);
            } else {
                return json()->data(['code' => -1, 'msg' => '添加失败']);
            }
        }
    }

    /**
     * 编辑上传文件组
     * @param SystemUploadGroupRepository $repository
     * @return \think\response\Json|void
     * @throws \think\db\exception\DbException
     */
    public function edit(SystemUploadGroupRepository $repository)
    {
        if ($this->request->isPost()) {
            $id = $this->request->param('id', 0, 'intval');
            $name = $this->request->param('name', '', 'trim');
            if (empty($name) || $id <= 0) {
                return json()->data(['code' => -1, 'msg' => '参数错误']);
            }
            if (!$repository->exists($id)) {
                return $this->error('数据不存在');
            }
            $info = $repository->get($id);

            $data = [
                'group_name' => $name,
                'pid' => $info['pid']
            ];
            $res = $repository->update($id, $data);
            if ($res) {
                return json()->data(['code' => 0, 'msg' => '编辑成功']);
            } else {
                return json()->data(['code' => -1, 'msg' => '编辑失败']);
            }
        }
    }

    /**
     * 删除上传文件组
     *
     * @param SystemUploadGroupRepository $repository
     * @return string|\think\response\Json|\think\response\View|void
     * @throws \think\db\exception\DbException
     */
    public function del(SystemUploadGroupRepository $repository)
    {
        if ($this->request->isPost()) {
            $id = $this->request->param('id', 0, 'intval');
            if ($id <= 0) {
                return json()->data(['code' => -1, 'msg' => '参数错误']);
            }
            if (!$repository->exists($id)) {
                return $this->error('数据不存在');
            }
            if ($repository->hasChild($id)) {
                return $this->error('该分组存在子分组，请先处理子分组');
            }
            $info = $repository->get($id);
            $res = $repository->delete($id);
            if ($res) {
                /**
                 * @var SystemUploadFileRepository $SystemUploadFileRepository
                 */
                $SystemUploadFileRepository = app()->make(SystemUploadFileRepository::class);
                $SystemUploadFileRepository->whereUpdate([
                    'group_id' => $id
                ], [
                    'group_id' => $info['pid']
                ]);
                return json()->data(['code' => 0, 'msg' => '删除成功']);
            } else {
                return json()->data(['code' => -1, 'msg' => '删除失败']);
            }
        }
    }


}